﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Linq;
using B2B.Bussiness;
using System.IO.IsolatedStorage;

namespace B2B.Views
{
    public partial class Login : Page
    {
        ServiceReference1.DataServiceClient client = new ServiceReference1.DataServiceClient();
        private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

        public Login()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Login_Loaded);
            txtFirmaKodu.KeyDown += txtFirmaKodu_KeyDown;
            txtSifre.KeyDown += new System.Windows.Input.KeyEventHandler(txtSifre_KeyDown);
            txtKullaniciAdi.KeyDown += new System.Windows.Input.KeyEventHandler(txtKullaniciAdi_KeyDown);
            client.HatirlatmaMailGonderCompleted += client_HatirlatmaMailGonderCompleted;
        }

        void client_HatirlatmaMailGonderCompleted(object sender, ServiceReference1.HatirlatmaMailGonderCompletedEventArgs e)
        {
            if (e.Result == "OK")
            {
                MessageBox.Show("Mail Gönderildi !");
            }
            else
            {
                MessageBox.Show("Mail Gönderilemedi !\r\n" + e.Result);
            }
        }

        void txtFirmaKodu_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
                txtKullaniciAdi.Focus();
        }

        void txtKullaniciAdi_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
                txtSifre.Focus();
        }

        void txtSifre_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                CheckLogin();
            }
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            ReadSettings();
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            txtFirmaKodu.Focus();
            client.DovizListesiAsync();
            client.DovizListesiCompleted += client_DovizListesiCompleted;
        }

        void client_DovizListesiCompleted(object sender, ServiceReference1.DovizListesiCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                Session.Instance.Add("USD", 0);
                Session.Instance.Add("EURO", 0);

                var usd = e.Result.Where(x => x.DovizCinsi == 1).FirstOrDefault();
                var eu = e.Result.Where(x => x.DovizCinsi == 2).FirstOrDefault();
                if (usd != null)
                {
                    txtDoviz.Text = "USD " + usd.Kur.ToString("n2");
                    Session.Instance.Add("USD", usd.Kur);
                }
                if (eu != null)
                {
                    txtDoviz.Text += "   EURO " + eu.Kur.ToString("n2");
                    Session.Instance.Add("EURO", eu.Kur);
                }
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void CheckLogin()
        {
            bsyIndic.IsBusy = true;
            client.KullaniciKontrolCompleted += client_KullaniciKontrolCompleted;
            client.KullaniciKontrolAsync(txtKullaniciAdi.Text, txtSifre.Password, txtFirmaKodu.Text);
        }

        private void SaveSettings()
        {
            if (!userSettings.Contains("firmakodu"))
            {
                userSettings.Add("firmakodu", txtFirmaKodu.Text);
            }
            else
            {
                userSettings["firmakodu"] = txtFirmaKodu.Text;
            }

            if (!userSettings.Contains("username"))
            {
                userSettings.Add("username", txtKullaniciAdi.Text);
            }
            else
            {
                userSettings["username"] = txtKullaniciAdi.Text;
            }

            if (!userSettings.Contains("password"))
            {
                userSettings.Add("password", txtSifre.Password);
            }
            else
            {
                userSettings["password"] = txtSifre.Password;
            }

            string hatirla = "0";
            if (chkHatila.IsChecked.Value)
                hatirla = "1";

            if (!userSettings.Contains("hatirla"))
            {
                userSettings.Add("hatirla", hatirla);
            }
            else
            {
                userSettings["hatirla"] = hatirla;
            }
        }

        private void ReadSettings()
        {
            if (userSettings.Contains("firmakodu"))
            {
                txtFirmaKodu.Text = userSettings["firmakodu"].ToString();
            }
            if (userSettings.Contains("username"))
            {
                txtKullaniciAdi.Text = userSettings["username"].ToString();
            }
            if (userSettings.Contains("password"))
            {
                txtSifre.Password = userSettings["password"].ToString();
            }
            if (userSettings.Contains("hatirla"))
            {
                if (userSettings["hatirla"].ToString() == "1")
                    chkHatila.IsChecked = true;
            }
        }

        void client_KullaniciKontrolCompleted(object sender, ServiceReference1.KullaniciKontrolCompletedEventArgs e)
        {
            bsyIndic.IsBusy = false;
            if (e.Result != null)
            {
                if (chkHatila.IsChecked.Value)
                {
                    SaveSettings();
                }
                else
                {
                    userSettings.Clear();
                }

                Session.Instance.Add("USER", e.Result);
                this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Lütfen bilgilerinizi kontrol edip tekrar deneyin !");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            CheckLogin();
        }

        private void SifreGonder(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtKullaniciAdi.Text))
            {
                client.HatirlatmaMailGonderAsync(txtKullaniciAdi.Text);
            }
        }
    }
}
